#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setWindowFlags(Qt::FramelessWindowHint);
    QWSServer::setCursorVisible( false );
    screenIndex = 0;

    /*clean screen which is result time before */

    /*Initialize listScreen Widget stack to contain screens*/
    listScreen = new QStackedWidget(this);
    listScreen->resize(320,240);

    //connect(this,SIGNAL(appLetDraw()),this,SLOT(drawFollowApp()));

    /*Enable to recieve Swipe gesture*/
    //grabGesture(Qt::SwipeGesture);

    drawRunTime();
//    testIntergrated();
//    qDebug()<<"gia tri debug la "<<setInt(3);
//    qDebug()<<"ip la "<<mlsGetIPv4();


}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    QPushButton* button = qobject_cast<QPushButton*>(sender());
     qDebug() <<"button "<<button->objectName()<<" da nhan \n";

    if (QString::compare(button->objectName(),"1", Qt::CaseInsensitive)==0)
    {
           qDebug() << "button 1b da press: \n ";
           ThreadCountUp *countUp = new ThreadCountUp();
           connect(countUp,SIGNAL(timeValue(int)),this, SLOT(displayChangeTimeIndex(int)));
           countUp->start();
    }
}

void MainWindow::initializeDataButton(button *a)
{
    a->text = "Manh Tien Sinh";
    a->pos_X = 200;
    a->pos_Y = 100;
    a->size = 50;
    a->colorRGB16 =0xFF2D;
    a->specFeature = "italic";
    a->font = "arial";
}

void MainWindow::drawButton(button *a)
{
    a->setGeometry(QRect(50, 50*1, 81, 31));
    QString s = QString::number(19);
    QByteArray ba = s.toLocal8Bit();
    a->setText(QApplication::translate("MainWindow",ba.data(), 0));
    a->setObjectName(s);
    a->show();
}


void MainWindow::initializeDataTextString(TextString * text)
{
    text->text = "Hello Manh ";
    text->name = "name";
    text->font = "times";
    text->size  = 50;
    text->pos_X = 150;
    text->pos_Y = 0;
    text->specFeature = "Bold";
    text->colorRGB16= 0xFFFF;
    qDebug() << "gia tri RGB:  "<<Qt::black<<"\n";
}


void MainWindow::drawRunTime()
{
    qDebug() << "enter \n ";
    QWidget *qwidgetList[2];
    for(int i = 0; i <2; i++)
    {
       qwidgetList[i] = new QWidget();
       qwidgetList[i]->resize(320,240);
    }

     grabGesture(Qt::SwipeGesture);
     listScreen = new QStackedWidget(this);
     listScreen->addWidget(qwidgetList[0]);
     listScreen->resize(320,240);

     qDebug()<<"Ryder Ta Cong Manh"<<listScreen->count();
//     this->listScreen->addWidget(qwidgetList[1]);

     pRecognizer = new SwipeGestureRecognizer();
     QGestureRecognizer::registerRecognizer(pRecognizer);


     //     drawFirstScreen(qwidgetList[0]);
     //     drawSecondScreen(qwidgetList[1]);

       graphicsView = new QGraphicsView*[7];
       maxIndexScreen = 7;
       screenIndex = 1;
       for(int i= 0; i<maxIndexScreen;i++)
       {
           graphicsView[i] = new QGraphicsView(qwidgetList[0]);
           graphicsView[i]->setObjectName(QString::fromUtf8("graphicsView"));
           graphicsView[i]->setGeometry(QRect(-170+230*i, 20, 200, 150));
           graphicsView[i]->setStyleSheet("background-image:url(/picture/"+QString::number(i)+".png)");
           graphicsView[i]->show();
       }

       qDebug()<<"tai sao die 123 \n";
       qDebug()<<"Ryder Ta Cong Manh"<<listScreen->count();

//     graphicsView = new QGraphicsView(qwidgetList[0]);
//     graphicsView->setObjectName(QString::fromUtf8("graphicsView"));
//     graphicsView->setGeometry(QRect(60, 20, 200, 150));
//     graphicsView->setStyleSheet("background-image:url(/picture/happy.png)");
//     graphicsView->show();

//     graphicsView1 = new QGraphicsView(qwidgetList[0]);
//     graphicsView1->setObjectName(QString::fromUtf8("graphicsView"));
//     graphicsView1->setGeometry(QRect(-170, 20, 200, 150));
//     graphicsView1->setStyleSheet("background-image:url(/picture/happy1.png)");
//     graphicsView1->show();

//     graphicsView2 = new QGraphicsView(qwidgetList[0]);
//     graphicsView2->setObjectName(QString::fromUtf8("graphicsView"));
//     graphicsView2->setGeometry(QRect(290, 20, 200, 150));
//     graphicsView2->setStyleSheet("background-image:url(/picture/happy2.png)");
//     graphicsView2->show();

//        qDebug()<<"Ryder here  "<< this->listScreen->currentIndex();
       listScreen->setCurrentIndex(0);

}
/*function for drawing screens*/

bool MainWindow::event(QEvent *event)
{
    qDebug() << "have some event "<< event->type();
    if (event->type() == QEvent::Gesture)
    {
    //    qDebug() << "this event gesture \n";
        return gestureEvent(static_cast<QGestureEvent*>(event));
    }
    return QWidget::event(event);
}

/*Swipe handler event */
//void MainWindow::swipeTriggered(QSwipeGesture *pSwipe)
//{
//     if (pSwipe->state() == Qt::GestureFinished) {
//        qDebug("Swipe angle: %f", pSwipe->swipeAngle());
//        switch (pRecognizer->GetHorizontalDirection(pSwipe)) {
//           case QSwipeGesture::Left:
//              screenIndex = (screenIndex+1)%2;
//              this->listScreen->setCurrentIndex(screenIndex);
//              break;
//           case QSwipeGesture::Right:
//              screenIndex = (screenIndex+1)%2;
//              this->listScreen->setCurrentIndex(screenIndex);
//              break;
//           default:
//              break;
//        }

//        switch (pRecognizer->GetVerticalDirection(pSwipe)) {
//           case QSwipeGesture::Up:
//              qDebug("Swipe Up detected");
//              break;
//           case QSwipeGesture::Down:
//              qDebug("Swipe Down detected");
//              break;
//           default:
//              break;
//        }
//     }
//}

void MainWindow::swipeTriggered(QSwipeGesture *pSwipe)
{
     QParallelAnimationGroup *group = new QParallelAnimationGroup;
     QPointer<QPropertyAnimation> animation;
     QPointer<QPropertyAnimation> animation2;
     QPointer<QPropertyAnimation> animation3;

     if (pSwipe->state() == Qt::GestureFinished) {
        qDebug("Swipe angle: %f", pSwipe->swipeAngle());
        switch (pRecognizer->GetHorizontalDirection(pSwipe)) {

            case QSwipeGesture::Left:
            if (screenIndex<(maxIndexScreen-1))
            {
                if (screenIndex>0)
                    graphicsView[screenIndex-1]->hide();

                graphicsView[screenIndex]->show();
                animation = new QPropertyAnimation(graphicsView[screenIndex], "geometry");
                animation->setDuration(300);
                animation->setStartValue(QRect(60, 20, 200, 150));
                animation->setEndValue(QRect(-170, 20, 200, 150));

                if ((screenIndex+1)<maxIndexScreen)
                {
                    graphicsView[screenIndex+1]->show();
                    animation2 = new QPropertyAnimation(graphicsView[screenIndex+1], "geometry");
                    animation2->setDuration(300);
                    animation2->setStartValue(QRect(290, 20, 200, 150));
                    animation2->setEndValue(QRect(60, 20, 200, 150));
                    group->addAnimation(animation2);
                }

                if ((screenIndex+2)<maxIndexScreen)
                {
                    graphicsView[screenIndex+2]->show();
                    animation3 = new QPropertyAnimation(graphicsView[screenIndex+2], "geometry");
                    animation3->setDuration(300);
                    animation3->setStartValue(QRect(520, 20, 200, 150));
                    animation3->setEndValue(QRect(290, 20, 200, 150));
                    group->addAnimation(animation3);
                }
                group->addAnimation(animation);
                group->start();
                if ((screenIndex+1)<maxIndexScreen)
                    screenIndex++;
            }
            break;

            case QSwipeGesture::Right:

            if (screenIndex >0)
            {
                if (screenIndex<(maxIndexScreen-1))
                    graphicsView[screenIndex+1]->hide();
                graphicsView[screenIndex]->show();
                animation = new QPropertyAnimation(graphicsView[screenIndex], "geometry");
                animation->setDuration(300);
                animation->setStartValue(QRect(60, 20, 200, 150));
                animation->setEndValue(QRect(290, 20, 200, 150));
                group->addAnimation(animation);
                if ((screenIndex-1)>=0)
                {
                    graphicsView[screenIndex-1]->show();
                    animation2 = new QPropertyAnimation(graphicsView[screenIndex-1], "geometry");
                    animation2->setDuration(300);
                    animation2->setStartValue(QRect(-170, 20, 200, 150));
                    animation2->setEndValue(QRect(60, 20, 200, 150));
                    group->addAnimation(animation2);
                }
                if ((screenIndex-2)>=0)
                {
                    graphicsView[screenIndex-2]->show();
                    animation3 = new QPropertyAnimation(graphicsView[screenIndex-2], "geometry");
                    animation3->setDuration(300);
                    animation3->setStartValue(QRect(-400, 20, 200, 150));
                    animation3->setEndValue(QRect(-170, 20, 200, 150));
                    group->addAnimation(animation3);
                }
                group->start();
                if ((screenIndex-1)>=0)
                    screenIndex--;
            }
            break;
            default:
            break;
        }

        switch (pRecognizer->GetVerticalDirection(pSwipe)) {
           case QSwipeGesture::Up:
//              qDebug("Swipe Up detected");
              break;
           case QSwipeGesture::Down:
//              qDebug("Swipe Down detected");
              break;
           default:
              break;
        }
     }
}
/*Gesture event handler*/
bool MainWindow::gestureEvent(QGestureEvent *event)
{
    qDebug() << __FUNCTION__ << event->type() << event->gestures();
    if (QGesture *swipe = event->gesture(Qt::SwipeGesture))
    {
        swipeTriggered(static_cast<QSwipeGesture *>(swipe));
    }
    return true;
}

/*Mouse double click event handler*/
void MainWindow::mouseDoubleClickEvent(QMouseEvent *)
{
//    qDebug() << "double click event \n";
//    rotationAngle = 0;
//    scaleFactor = 1;
//    currentStepScaleFactor = 1;
//    verticalOffset = 0;
//    horizontalOffset = 0;
//    update();
}

/*Paint event handler*/
void MainWindow::paintEvent(QPaintEvent*)
{
//    QPainter p(this);

//    float iw = currentImage.width();
//    float ih = currentImage.height();
//    float wh = height();
//    float ww = width();

//    p.translate(ww/2, wh/2);
//    p.translate(horizontalOffset, verticalOffset);
//    p.rotate(rotationAngle);
//    p.scale(currentStepScaleFactor * scaleFactor, currentStepScaleFactor * scaleFactor);
//    p.translate(-iw/2, -ih/2);
//    p.drawImage(0, 0, currentImage);
}

/*Function draw screen for test*/
void MainWindow::drawFirstScreen(QWidget *widget)
{
    QFont font1;
    QLabel *label_2;
    font1.setPointSize(25);
    label_2 = new QLabel(widget);
    label_2->setObjectName(QString::fromUtf8("label_1"));
    label_2->setGeometry(QRect(0,160,320,40));
    label_2->setFont(font1);
    label_2->setText("This is potential project");
    label_2->show();
    this->listLable = label_2;

    QString s;
    QByteArray ba;
    QPushButton *buttonList[3];
    QFont font;
    font.setPointSize(40);
    for(int i = 0; i<3; i++)
    {
        buttonList[i] = new QPushButton(widget);
        buttonList[i]->setFont(font);
        buttonList[i]->setGeometry(QRect(100*i, 205, 81, 35));
        s = QString::number(i);
        ba = s.toLocal8Bit();
        buttonList[i]->setText(QApplication::translate("MainWindow",ba.data(), 0));
        buttonList[i]->setObjectName(s);
        buttonList[i]->show();

        connect( buttonList[i],SIGNAL(clicked(bool)),this,SLOT(on_pushButton_clicked()));
    }

    QGraphicsView *graphicsView_2;
    graphicsView_2 = new QGraphicsView(widget);
    graphicsView_2->setObjectName(QString::fromUtf8("graphicsView_1"));
    graphicsView_2->setGeometry(QRect(0, 0, 320, 160));
    graphicsView_2->setStyleSheet("background-image:url(/picture/01.png)");
    graphicsView_2->show();
}

/*Draw second screen for testing*/
void MainWindow::drawSecondScreen(QWidget *widget)
{
    QFont font1;
    QLabel *label_2;
    font1.setPointSize(25);
    label_2 = new QLabel(widget);
    label_2->setObjectName(QString::fromUtf8("label_2"));
    label_2->setGeometry(QRect(0,160,320,40));
    label_2->setFont(font1);
    label_2->setText("HEY, Ryder how are you?");
    label_2->show();

    QString s;
    QByteArray ba;
    QPushButton *buttonList[3];
    QFont font;
    font.setPointSize(40);
    for(int i = 3; i<6; i++)
    {
        buttonList[i-3] = new QPushButton(widget);
        buttonList[i-3]->setFont(font);
        buttonList[i-3]->setGeometry(QRect(100*(i-3), 205, 81, 35));
        s = QString::number(i);
        ba = s.toLocal8Bit();
        buttonList[i-3]->setText(QApplication::translate("MainWindow",ba.data(), 0));
        buttonList[i-3]->setObjectName(s);
        buttonList[i-3]->show();
        connect( buttonList[i-3],SIGNAL(clicked(bool)),this,SLOT(on_pushButton_clicked()));
    }

    QGraphicsView *graphicsView_2;
    graphicsView_2 = new QGraphicsView(widget);
    graphicsView_2->setObjectName(QString::fromUtf8("graphicsView_2"));
    graphicsView_2->setGeometry(QRect(0, 0, 320, 160));
    graphicsView_2->setStyleSheet("background-image:url(/picture/02.png)");
    graphicsView_2->show();
}
/*slot for handle drawing*/
void MainWindow::drawFollowApp()
{
    drawRunTime();
}

/*this function use for clear screen*/
void MainWindow::clearScreen(QWidget *w)
{
    QGraphicsView *graphicsView_2;
    graphicsView_2 = new QGraphicsView(w);
    graphicsView_2->setObjectName(QString::fromUtf8("graphicsView_2"));
    graphicsView_2->setGeometry(QRect(0, 0, 320, 240));
    graphicsView_2->setStyleSheet("color: white");
    graphicsView_2->show();

    QFont font1;
    QLabel *label_2;
    font1.setPointSize(25);
    label_2 = new QLabel(w);
    label_2->setObjectName(QString::fromUtf8("label_2"));
    label_2->setGeometry(QRect(30,30,320,40));
    label_2->setFont(font1);
    label_2->setText("WAITING...");
    label_2->show();
}

/*function event handler when form is fully load*/
void MainWindow::showEvent( QShowEvent *event )
{
   //  emit appLetDraw();
}

/*Function to display label indicate about time change each min*/
void MainWindow::displayChangeTimeIndex(int time)
{
    this->listLable->setText(QString::number(time));
}

/*Function to draw card Absent or Present*/
void MainWindow::drawCardState(QWidget *widget, QString picString)
{
    QGraphicsView *graphicsView_2;
    graphicsView_2 = new QGraphicsView(widget);
    graphicsView_2->setObjectName(QString::fromUtf8("graphicsView_1"));
    graphicsView_2->setGeometry(QRect(0, 0, 320, 240));
    graphicsView_2->setStyleSheet("background-image:url(/picture/" +picString+ ")");
    graphicsView_2->show();
}

/*Function to draw two screen of card state*/
void MainWindow::testIntergrated()
{
    qDebug() << "draw for intergated testing  \n ";
    QWidget *qwidgetList[2];
    for(int i = 0; i <2; i++)
    {
       qwidgetList[i] = new QWidget;
       qwidgetList[i]->resize(320,240);
    }

     grabGesture(Qt::SwipeGesture);
     this->listScreen->addWidget(qwidgetList[0]);
     this->listScreen->addWidget(qwidgetList[1]);

     pRecognizer = new SwipeGestureRecognizer();
     QGestureRecognizer::registerRecognizer(pRecognizer);

     drawCardState(qwidgetList[1],"cardPresent.png");
     drawCardState(qwidgetList[0],"cardAbsent.png");

     this->listScreen->setCurrentIndex(0);

//     ThreadNfcReader *threadNfc = new ThreadNfcReader();
//     threadNfc->start();

//     connect(threadNfc,SIGNAL(SignalCardAbsent()),this,SLOT(slotHandleCardAbsent()));
//     connect(threadNfc,SIGNAL(SignalCardPresent()),this,SLOT(slotHandleCardPresent()));

      ThreadToApp *threadApp = new ThreadToApp();
      threadApp->start();

      ThreadCheckCardState *threadCheckCard = new ThreadCheckCardState();
      threadCheckCard->start();

}

/*Slot function handle for card absent*/
void MainWindow::slotHandleCardAbsent()
{
     this->listScreen->setCurrentIndex(0);
}

/*Slot function handle for card present*/
void MainWindow::slotHandleCardPresent()
{
     this->listScreen->setCurrentIndex(1);
}
